import 'dart:io';
import 'dart:ui';

import 'package:flutter_app_info/device_info_plus.dart';
import 'package:flutter_app_info/package_info_plus.dart';
import 'package:jet_rails/jr_utils/jr_shared_preferences.dart';
import 'package:uuid/uuid.dart';

class JrDevice {
  static const String TAG = 'JrDevice';

  static Future<void> init() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    BaseDeviceInfo deviceInfo = await DeviceInfoPlugin().deviceInfo;

    _packageInfo = packageInfo;
    _deviceInfo = deviceInfo;

    String? uuid = JrSP.instance.getString('deviceId');
    if (uuid == null) {
      uuid = const Uuid().v4();
      JrSP.instance.setString('deviceId', uuid);
    }
    localDeviceId = uuid;
  }

  static late PackageInfo _packageInfo;

  static late BaseDeviceInfo _deviceInfo;

  static late String localDeviceId;

  static Map<String, dynamic> get _deviceInfoMap => _deviceInfo.data;

  /// device properties
  static String get platform => Platform.isAndroid ? 'Android' : 'iOS';

  static String get deviceId => _deviceInfoMap['identifierForVendor'] ?? localDeviceId;

  static String get model => _deviceInfoMap['model'] ?? 'iPhone';

  static String get packageName => _packageInfo.packageName;

  static String get version => _packageInfo.version;

  static String get build => _packageInfo.buildNumber;

  static String get language => window.locale.languageCode;
}
